package com.m3958.firstgwt.server.dao;

import java.util.Date;
import java.util.List;

import com.m3958.firstgwt.client.types.ServerErrorCode;
import com.m3958.firstgwt.server.model.User;
import com.m3958.firstgwt.server.model.Vote;
import com.m3958.firstgwt.server.types.SmartError;
import com.m3958.firstgwt.server.types.TreeModel;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartConstants;


public class VoteChangeStrategy  extends TreeChangeStrategy implements ModelChangeStrategy<Vote> {
	
	
	@Override
	public boolean extraPersistTask(Vote model) {
		if(model.getParent() != null){
			if(!isAllowed(model.getParent())){
				return false;
			}
		}
		if(!su.isSuperman()){
			model.setPaid(false);
		}
		if(model.getMinSelect() == 0){
			model.setMinSelect(1);
		}
		if(model.getMaxSelect() == 0){
			model.setMaxSelect(1);
		}
		if(model.getMinSelect() > model.getMaxSelect()){
			int max = model.getMinSelect();
			model.setMinSelect(model.getMaxSelect());
			model.setMaxSelect(max);
		}
		return checkDuplication(model);
	}
	
	private boolean checkDuplication(Vote model) {
		if(model.getParent() == null){
			VoteDao vdao = injector.getInstance(VoteDao.class);
			int siteId = getReqPs().getIdValue(CommonField.SITE_ID.getValue());
			List<Vote> ss;
			if(siteId == SmartConstants.NONE_EXIST_MODEL_ID){
				ss = vdao.getUserTopVotes(getSu().getUserId());
			}else{
				ss = vdao.getSiteTopVotes(siteId);
			}
			 
			for(Vote s : ss){
				if(s.getName().equals(model.getName())){
					getErrors().addError(new SmartError("同级目录不能重名！", ServerErrorCode.DUPLICATE_RECORD));
					return false;
				}
			}
		}else{
			int i=0;
			for(TreeModel<Vote> s :model.getParent().getChildren()){
				if(((Vote) s).getName().equals(model.getName())){
					i++;
				}
			}
			if(i>1){
				getErrors().addError(new SmartError("同级目录不能重名！", ServerErrorCode.DUPLICATE_RECORD));
				model.getParent().removeChildren(model);
				return false;
			}
		}
		return true;
		
	}



	@Override
	public boolean extraUpdateTask(Vote model,Vote newModel) {//30天之后不能修改付费投票。
		if(!isAllowed(model))return false;
		if(!su.isSuperman()){
			newModel.setPaid(model.isPaid());
		}
		if(model.getCreator() == null && getSu().getLoginStatus()){
			User u = emp.get().find(User.class, getSu().getUserId());
			model.setCreator(u);
		}
		return true;
	}
	
	private boolean isAllowed(Vote vote){
		Vote v = getPaidVote(vote);
		if(v != null){
			if((new Date().getTime() - v.getCreatedAt().getTime()) > 30*24*3600){
				getErrors().addError(new SmartError("30天之后不能修改付费投票", 30));
				return false;
			}
		}
		return true;
	}
	
	private Vote getPaidVote(Vote vote){
		Vote v = vote;
		if(v.isPaid())return v;
		while(v.getParent() != null){
			v = v.getParent();
			if(v.isPaid())return v;
		}
		return null;
	}


	@Override
	public boolean extraRemoveTask(Vote model) {
		return true;
	}
	

	@Override
	public boolean afterPersist(Vote model) {
		return false;
	}
}
